﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ControlLibary
{
    public class DataGridViewEx : DataGridView
    {
        /// <summary>
        /// Ctor
        /// </summary>
        public DataGridViewEx()
        {
            //开启双缓冲  优化DataGridView显示效果
            //Type dgvType = this.GetType();
            //PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            //pi.SetValue(this, true, null);

            //注:在.net framework 4.5及以前的版本中  DoubleBuffered属性未开放  需要用上面的方式开启双缓冲
            this.DoubleBuffered = true; //开启双缓冲

            this.BackgroundColor = Color.FromArgb(238, 238, 242);

            this.BackColor = Color.FromArgb(238, 238, 242); //设置单元格默认的背景颜色

            this.GridColor = Color.FromArgb(209, 209, 209); //设置单元格边框颜色

            this.BorderStyle = BorderStyle.None;

            this.AllowUserToAddRows = false; //禁止自动添加行

            this.EnableHeadersVisualStyles = false; //设置行头列头不使用默认样式  否则即使设置了行头列头样式也不会起作用 

            this.RowHeadersDefaultCellStyle.BackColor = Color.FromArgb(238, 238, 242); //设置行头颜色

            this.RowHeadersDefaultCellStyle.SelectionBackColor = Color.FromArgb(219, 219, 219);//设置行头选中时的背景颜色

            this.RowHeadersDefaultCellStyle.SelectionForeColor = Color.FromArgb(0, 0, 0);//设置行头选中时的前景颜色

            this.RowHeadersWidth = 30; //设置行头宽度 

            this.RowTemplate.Height = 23;//设置默认行高

            this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; //禁止修改行头宽度

            this.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; //设置行头边框样式

            this.RowHeadersDefaultCellStyle.Padding = new Padding(this.RowHeadersWidth); //隐藏行头小箭头



            this.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(238, 238, 242); //设置列头颜色

            this.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.FromArgb(219, 219, 219);//设置列头选中时的背景颜色

            this.ColumnHeadersDefaultCellStyle.ForeColor = Color.FromArgb(0, 0, 0);//设置列头选中时的前景颜色

            this.ColumnHeadersHeight = 23; //设置列头高度   

            this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; //禁止修改列头高度

            this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; //设置列头边框样式

            this.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //设置列头文本居中对齐

            this.DefaultCellStyle.ForeColor = Color.FromArgb(0, 0, 0); //设置单元格默认的前景颜色

            this.DefaultCellStyle.SelectionForeColor = Color.FromArgb(0, 0, 0);//设置单元格选中时的前景颜色

            this.DefaultCellStyle.SelectionBackColor = Color.FromArgb(192, 192, 192); //设置单元格选中时的背景颜色

            this.DefaultCellStyle.BackColor = Color.FromArgb(238, 238, 242); //设置单元格默认背景颜色

            //设置单元格文本自动换行  这里要搭配AutoSizeRowsMode属性一起使用
            this.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //this.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.False; 
        }

        /// <summary>
        /// 绘制行号
        /// </summary>
        /// <param name="e"></param>
        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
            using (SolidBrush b = new SolidBrush(this.RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString
                (
                    Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentUICulture),
                    e.InheritedRowStyle.Font, b,
                    e.RowBounds.Location.X + 5 - ((e.RowIndex + 1).ToString().Length - 1) * 2,
                    e.RowBounds.Location.Y + (e.RowBounds.Height - e.InheritedRowStyle.Font.Height) / 2
                );
            }
        }
    }
}
